package nlp.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import nlp.model.Message;
import nlp.utils.DatabasePool;

public class MessageDao {
	
	private Connection conn;
	private PreparedStatement psmt;
	private ResultSet rs;
	
	public boolean insert(Message msg){
		long id = msg.getId();
		Message old = selectMessageById(id);
		if(old!=null){
			
		}else{
			insertMessage(msg);
		}
		return true;
	}
	
	private boolean insertMessage(Message msg){
		conn=DatabasePool.getConnection();
		String sql="insert into message(id,text,createat,rtid,rttext,rtcreateat) values(?,?,?,?,?,?)";
		try {
			psmt=conn.prepareStatement(sql);
			psmt.setLong(1, msg.getId());
			psmt.setString(2,msg.getText());
			psmt.setString(3,msg.getCreateAt());
			psmt.setLong(4, msg.getRtId());
			psmt.setString(5, msg.getRtText());
			psmt.setString(6, msg.getRtCreateAt());
			
			psmt.execute();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return true;
	}
	
	/**
	 * 暂时保留
	 * @return
	 * @author Chenjie
	 */
	public List<Message> selectMessageByUId(long uid){
		conn=DatabasePool.getConnection();
		String sql="select * from message ";
		List<Message> msgList=null;
		try {
			psmt=conn.prepareStatement(sql);
			rs=psmt.executeQuery();
			msgList=queryListFromResultSet(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return msgList;
	}
	
	public Message selectMessageById(long id){
		conn=DatabasePool.getConnection();
		String sql="select * from message where id = ?";
		Message msg=null;
		try {
			psmt=conn.prepareStatement(sql);
			psmt.setLong(1, id);
			rs=psmt.executeQuery();
			msg=queryFromResultSet(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return msg;
	}
	private List<Message> queryListFromResultSet(ResultSet rs){
		List<Message> msgList=new ArrayList<Message>();
		Message msg=null;
		try {
			while(rs.next()){
				msg=new Message();
				msg.setId(rs.getLong("id"));
				msg.setText(rs.getString("text"));
				msg.setCreateAt(rs.getString("createat"));
				msg.setRtId(rs.getLong("rtid"));
				msg.setRtText(rs.getString("rttext"));
				msg.setRtCreateAt(rs.getString("rtcreateat"));
				msgList.add(msg);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return msgList;
	}
	
	private Message queryFromResultSet(ResultSet rs){
		Message msg=null;
		try {
			if(rs.next()){
				msg=new Message();
				msg.setId(rs.getLong("id"));
				msg.setText(rs.getString("text"));
				msg.setCreateAt(rs.getString("createat"));
				msg.setRtId(rs.getLong("rtid"));
				msg.setRtText(rs.getString("rttext"));
				msg.setRtCreateAt(rs.getString("rtcreateat"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		
		
		return msg;
	}

	private void close(){
		try {
			if(rs!=null){
					rs.close();
			}
			if(psmt!=null){
				psmt.close();
			}
			if(conn!=null){
				conn.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
